package com.ljq.software_springboot.softwareprogram.controller;


import com.ljq.software_springboot.softwareprogram.entity.Account;
import com.ljq.software_springboot.softwareprogram.entity.ChatMember;
import com.ljq.software_springboot.softwareprogram.entity.Chatgroup;
import com.ljq.software_springboot.softwareprogram.entity.GroupRequest;
import com.ljq.software_springboot.softwareprogram.imp.ChatgroupServiceImpl;
import com.ljq.software_springboot.softwareprogram.mapper.AccountMapper;
import com.ljq.software_springboot.softwareprogram.mapper.ChatMemberMapper;
import com.ljq.software_springboot.softwareprogram.mapper.ChatgroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@RestController
@RequestMapping("/chatgroup")
@CrossOrigin(origins = "http://localhost:5173")
public class ChatgroupController {

    @Autowired
    private ChatgroupMapper chatgroupMapper;
    @Autowired
    private ChatMemberMapper chatMemberMapper;
    @Autowired
    private ChatgroupServiceImpl chatgroupService;
    @PostMapping("/create-group")
    public ResponseEntity<?> createGroup(@RequestBody GroupRequest groupRequest) {
        // 创建群组
        Chatgroup userGroup = new Chatgroup();
        userGroup.setName(groupRequest.getGroupName());
        userGroup.setCreatorId(groupRequest.getCreatorId());
        chatgroupMapper.insert(userGroup);

        // 将群主加入群组
        ChatMember groupMember = new ChatMember();
        groupMember.setGroupId(userGroup.getId());
        groupMember.setUserId(groupRequest.getCreatorId());
        chatMemberMapper.insert(groupMember);

        return ResponseEntity.ok("群组创建成功");
    }
    @PostMapping("/getcreatorgroups")
    public List<?> getcreatorGroup(@RequestBody Chatgroup  chatgroup) {
        return chatgroupService.findAll(chatgroup.getCreatorId());
    }
    @PostMapping("/getgroups")
    public List<?> getGroup(@RequestBody Chatgroup  chatgroup) {
        return chatgroupService.findgroup(chatgroup.getCreatorId());
    }
    @PostMapping("/findid")
    public Integer findid(@RequestBody Chatgroup body) {
        return chatgroupService.findId(body.getName());
    }
}
